import re

defaultMinWordLength = 3

_validWordPattern = re.compile("[A-Za-z_]\w*$")
_repchars = ".():@"

def splitText(txt, minWordLength = defaultMinWordLength):
	for repchar in _repchars:
		txt = txt.replace(repchar, " ")
	candidates = txt.split()
	return [w for w in candidates
		if len(w) >= minWordLength and _validWordPattern.match(w)]


class HoheWordCompleter(object):
	def __init__(self, minWordLength = defaultMinWordLength):
		self.words = set()
		self.keywords = set()
		self.minWordLength = minWordLength
		
	def setKeywords(self, keywords):
		self.keywords = set(keywords)
		
	def setText(self, txt):
		""" Set keywords """
		words = splitText(txt, self.minWordLength)
		self.words = set(words)
		
	def getWords(self, partOfWord):
		wlength = len(partOfWord)
		words = [(len(w), w) for w in (self.words.union(self.keywords))
			if w[:wlength] == partOfWord and len(w) != len(partOfWord)]
		words.sort()
		if isinstance(partOfWord, unicode):
			return [unicode(w[1]) for w in words]
		else:
			return [w[1] for w in words]


if __name__ == "__main__":

	testText = """konnnakoto piii
	piiii konnayume pii oo. kk.kk kk.aiue kk.kaiue"""

	hwc = HoheWordCompleter()
	hwc.setText(testText)
	print (hwc.getWords("k") == ['kaiue', 'konnayume', 'konnnakoto'])
	print (hwc.getWords(u"k") == [u'kaiue', u'konnayume', u'konnnakoto'])

